package com.hnust.wswx.moblieStuBack.controller;

import com.hnust.wswx.moblieStuBack.service.imgUploadService;
import com.hnust.wswx.moblieStuBack.util.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.sql.Array;
import java.util.*;

@CrossOrigin("*") //解决跨域问题
@RestController
@RequestMapping("/test")
@Slf4j
public class imgUploadController {

    @Value("${pic.url}")
    private String path;

    private final imgUploadService imgUploadService;

    public imgUploadController(imgUploadService imgUploadService) {
        this.imgUploadService = imgUploadService;
    }

    @CrossOrigin(origins = "*", maxAge = 3600)
    @PostMapping(value = "/upload", headers = "content-type=multipart/form-data")
    public R<Object> upload(HttpServletRequest request) {
        MultipartHttpServletRequest r = (MultipartHttpServletRequest) request;
        //map键值接收文件列表
        Map<String, MultipartFile> map = r.getFileMap();
        System.out.println(map.values().getClass());
        //将MultipartFile以列表形式储存
        int t = 0;
        MultipartFile[] multipartFileList = new MultipartFile[map.size()];
        for (String key : map.keySet()) {
            multipartFileList[t] = map.get(key);
            System.out.println("遍历后的files" + multipartFileList[t]);
            t++;
        }
        //定义传入的文件个数
        int length = multipartFileList.length;
        System.out.println("前端传来了" + length + "张图片");
        //文件名
        String[] file_Name = new String[length];
        //文件后缀
        String[] ext_Name = new String[length];
        //
        ArrayList<String > list = new ArrayList<>();
        try {
            if (length > 0) {
                for (int i = 0; i < length; i++) {
                    System.out.println("第" + i + "个原文件名:" + multipartFileList[i].getOriginalFilename());
                    file_Name[i] = Objects.requireNonNull(multipartFileList[i].getOriginalFilename()).split("\\.")[0];
                    System.out.println(i + "文件名: " + file_Name[i]);
                    ext_Name[i] = Objects.requireNonNull(multipartFileList[i].getOriginalFilename()).split("\\.")[1];
                    byte[] content = multipartFileList[i].getBytes();
                    //可访问路径
                    StringBuilder filePath = new StringBuilder();
                    //file的storagePath,ext_Name[i]
                    String SP_file = imgUploadService.upLoad(content, ext_Name[i], file_Name[i]);
                    //拼接返回的图片地址的ip地址
                    filePath.append("http://").append(path).append("/");
                    //拼接返回图片的存储地址
                    filePath.append(SP_file);
                    //现在开始拼接file的浏览器可访问地址
                    System.out.println("这里是第" + i + "个file可访问路径:" + filePath.toString());
                    list.add(filePath.toString());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        String ListToStr = String.join(",",list);
        return R.success(ListToStr);
    }
}
